﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBatisNet.DataMapper;
using StateGrid.OperationLog;
using StateGrid;
using System.Xml;
using StateGrid.GlobalVariables;

namespace StateGrid.ZCWX
{
    /// <summary>
    ///ZCWX_DAO 的摘要说明
    /// </summary>
    public class ZCWX_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();
        
        /// <summary>
        /// 日志操作对象
        /// </summary>
        private OperationLog_DAO operationLogDAO = new OperationLog_DAO();

        /// <summary>
        /// 获取所有自产文献分类
        /// </summary>
        public IList<ZCWX_FL> GetAllZCWX_FL()
        {
            return sqlMapper.QueryForList<ZCWX_FL>("SelectAllZCWX_FL", null);
        }

        /// <summary>
        /// 获取所有自产文献篇名
        /// </summary>
        public IList<ZCWX_PM> GetAllZCWX_PM()
        {
            return sqlMapper.QueryForList<ZCWX_PM>("SelectAllZCWX_PM", null);
        }

        /// <summary>
        /// 按分类编码查找分类信息
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public ZCWX_FL GetZCWX_FLByFlbm(String flbm)
        {
            return sqlMapper.QueryForObject<ZCWX_FL>("SelectZCWX_FLByFlbm", flbm);
        }

        /// <summary>
        /// 按照分类编码名称查找分类信息
        /// </summary>
        /// <param name="flmc">分类名称</param>
        /// <returns></returns>
        public ZCWX_FL GetZCWX_FLByFlmc(String flmc)
        {
            return sqlMapper.QueryForObject<ZCWX_FL>("SelectZCWX_FLByFlmc", flmc);
        }

        /// <summary>
        /// 按照单位代码查找分类信息
        /// </summary>
        /// <param name="dwdm">分类名称</param>
        /// <returns></returns>
        public ZCWX_FL GetZCWX_FLByDwdm(String dwdm)
        {
            return sqlMapper.QueryForObject<ZCWX_FL>("SelectZCWX_FLByDwdm", dwdm);
        }

        /// <summary>
        /// 获取指定分类编码、出版年份的篇名数量
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="cbnf">出版年份</param>
        public int GetCountSomeZCWX_PM(String flbm, String cbnf)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String sybm = null;

            if (null == flbm || "".Equals(flbm))
            {
            }
            else
            {
                //根据分类编码获得单位代码
                ZCWX_FL zcwx = GetZCWX_FLByFlbm(flbm);
                if (null != zcwx.dwdm)
                {
                    sybm = zcwx.dwdm;
                }
                if (null != cbnf && cbnf.Length == 4)
                {
                    sybm += '-' + cbnf;
                }
            }

            if (null != sybm)
            {
                sybm += '%';
            }

            para.Add("sybm", sybm);
            return sqlMapper.QueryForObject<int>("CountSomeZCWX_PM", para);
        }

        /// <summary>
        /// 获取指定分类编码、出版年份的篇名数量
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="cbnf">出版年份</param>
        /// <param name="start">起始位置</param>
        /// <param name="end">结束位置</param>
        /// <returns></returns>
        public IList<ZCWX_PM> GetSomeZCWX_PM(String flbm, String cbnf, int start, int end)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String sybm = null;

            if (null == flbm || "".Equals(flbm))
            {
            }
            else
            {
                ZCWX_FL zcwx = GetZCWX_FLByFlbm(flbm);
                if (null != zcwx.dwdm)
                {
                    sybm = zcwx.dwdm;
                }
                if (null != cbnf && cbnf.Length == 4)
                {
                    sybm += '-' + cbnf;
                }
            }

            if (null != sybm)
            {
                sybm += '%';
            }

            para.Add("sybm", sybm);
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<ZCWX_PM>("SelectSomeZCWX_PM", para);
        }

        /// <summary>
        /// 获取指定单位编码、出版年份的篇名数量
        /// </summary>
        /// <param name="dwbm">单位编码</param>
        /// <param name="cbnf">出版年份</param>
        /// <param name="start">起始位置</param>
        /// <param name="end">结束位置</param>
        /// <returns></returns>
        public IList<ZCWX_PM> GetSomeZCWX_PM_ByDwdm(String dwdm, String cbnf, int start, int end)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String sybm = null;

            if (null == dwdm || "".Equals(dwdm))
            {
            }
            else
            {
                sybm = dwdm;
                if (null != cbnf && cbnf.Length == 4)
                {
                    sybm += '-' + cbnf;
                }
            }

            if (null != sybm)
            {
                sybm += '%';
            }

            para.Add("sybm", sybm);
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<ZCWX_PM>("SelectSomeZCWX_PM", para);
        }

        /// <summary>
        /// 获取指定索引编码的篇名信息
        /// </summary>
        /// <param name="sybm">索引编码</param>
        /// <returns></returns>
        public ZCWX_PM GetZCWX_PMBySybm(String sybm)
        {
            return sqlMapper.QueryForObject<ZCWX_PM>("SelectZCWX_PMBySybm", sybm);
        }

        /// <summary>
        /// 获取指定中文篇名和出版单位的篇名信息
        /// </summary>
        /// <param name="zwpm">中文篇名</param>
        /// <param name="cbdw">出版单位</param>
        /// <returns></returns>
        public ZCWX_PM GetZCWX_PMByZwpmAndCbdw(String zwpm, String cbdw)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("zwpm", zwpm);
            para.Add("cbdw", cbdw);

            return sqlMapper.QueryForObject<ZCWX_PM>("SelectZCWX_PMByZwpmAndCbdw", para);
        }

        /// <summary>
        /// 插入一条分类信息
        /// </summary>
        /// <param name="zcwx_fl">分类信息</param>
        /// <returns></returns>
        public Boolean InsertZCWX_FL(ZCWX_FL zcwx_fl)
        {
            Boolean flag = true;
            try
            {
                sqlMapper.Insert("InsertZCWX_FL", zcwx_fl);
            }
            catch (Exception)
            {
                flag = false;
            }

            return flag;
        }

        /// <summary>
        /// 插入一条篇名信息
        /// </summary>
        /// <param name="zcwx_pm">篇名信息</param>
        /// <returns></returns>
        public Boolean InsertZCWX_PM(ZCWX_PM zcwx_pm)
        {
            Boolean flag = true;
            OperationLogItem operationLog = this.GetOperationLogItem(zcwx_pm, "INSERT", null);
            sqlMapper.Insert("InsertZCWX_PM", zcwx_pm);
            this.operationLogDAO.insertLogItem(operationLog);
            return flag;
        }

        /// <summary>
        /// 更新一条分类信息
        /// </summary>
        /// <param name="zcwx_fl">要更新的分类信息</param>
        /// <returns></returns>
        public Boolean UpdateZCWX_FL(ZCWX_FL zcwx_fl)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateZCWX_FL", zcwx_fl);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;

        }
        /// <summary>
        /// 更新一条篇名信息
        /// </summary>
        /// <param name="zcwx_pm">要更新的篇名信息</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        public Boolean UpdateZCWX_PM(ZCWX_PM zcwx_pm, String uname)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateZCWX_PM", zcwx_pm);
            if (back <= 0)
            {
                flag = false;
            }
            if (flag)
            {
                OperationLogItem operationLog = this.GetOperationLogItem(zcwx_pm, "UPDATE", uname);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            return flag;
        }

        /// <summary>
        /// 按分类编码删除分类信息
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <returns></returns>
        public Boolean DeleteZCWX_FL(String flbm)
        {
            Boolean flag = true;
            int back = sqlMapper.Delete("DeleteZCWX_FLByFlbm", flbm);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 按索引编码删除篇名信息
        /// </summary>
        /// <param name="sybm">索引编码</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>

        public int DeleteZCWX_PM(String sybm, String uname)
        {
            int result = 0;

            ZCWX_PM zcwxPM = this.GetZCWX_PMBySybm(sybm);

            OperationLogItem operationLog = null;


            //切分索引编码
            String[] str = sybm.Split('-');
            if (str.Length < 3)
            {
                return result;
            }
            String prefix = str[0] + "-" + str[1] + "-";

            result = sqlMapper.Delete("DeleteZCWX_PMBySybm", sybm);
            operationLog = this.GetOperationLogItem(zcwxPM, "DELETE", uname);
            if (null != operationLog)
            {
                this.operationLogDAO.insertLogItem(operationLog);
            }

            ICollection<ZCWX_PM> list = this.GetSomeZCWX_PM_ByDwdm(str[0], str[1], 1, 10000);

            int ph = 1;
            String phString = null;
            if (null != list)
            {
                foreach (ZCWX_PM temp in list)
                {
                    phString = FormatPH(ph++);
                    //如果当前索引编码与新索引编码一样则不做处理
                    if (temp.sybm.Equals(prefix + phString))
                    {
                        continue;
                    }
                    //如果当前索引编码与新索引编码不一致,则更新索引编码及篇名信息
                    else
                    {
                        changeZCWXPMSybm(temp.sybm, prefix + phString);
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 修改已有的索引编码。
        /// </summary>
        /// <param name="oldWjm">旧文件名</param>
        /// <param name="newWjm">新文件名</param>
        /// <returns>是否修改成功</returns>
        public bool changeZCWXPMSybm(String oldSybm, String newSybm)
        {
            if (null == oldSybm || null == newSybm)
            {
                return false;
            }

            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("oldSybm", oldSybm);
            para.Add("newSybm", newSybm);

            bool result = true;
            try
            {
                sqlMapper.Update("ChangeZCWX_PM_sybm", para);
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 规范篇号
        /// </summary>
        /// <param name="ph">篇号</param>
        /// <returns>4位篇号</returns>
        private String FormatPH(int ph)
        {
            String phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "000" + phString;
            }
            //长度为2
            else if (phString.Length == 2)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 3)
            {
                phString = "0" + phString;
            }
            return phString;
        }

        /// <summary>
        /// 根据分类编码、出版年份获取最大文件的篇名信息
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="cbnf">出版年份</param>
        /// <returns></returns>
        public ZCWX_PM getMaxZCWX_PM(String flbm, String cbnf)
        {
            if (null == flbm || "".Equals(flbm))
            {
                return null;
            }
            if (null == cbnf || "".Equals(cbnf))
            {
                return null;
            }

            String sybm = null;

            //设置匹配索引编码
            ZCWX_FL zcwx = this.GetZCWX_FLByFlbm(flbm);
            if (null != zcwx.dwdm)
            {
                sybm = zcwx.dwdm;
            }

            sybm = sybm + "-" + cbnf + "%";

            return sqlMapper.QueryForObject<ZCWX_PM>("SelectMaxZCWX_PM", sybm);
        }

        /// <summary>
        /// 生成日志信息
        /// </summary>
        /// <param name="zwqkPM">中文篇名信息</param>
        /// <param name="opType">操作类型</param>
        /// <param name="uname">用户名</param>
        /// <returns></returns>
        private OperationLogItem GetOperationLogItem(ZCWX_PM zcwxPM, String opType, String uname)
        {
            if (null == zcwxPM)
            {
                return null;
            }

            OperationLogItem operationLogItem = new OperationLogItem();
            //操作类型
            operationLogItem.opType = opType;
            //数据库表
            operationLogItem.opTable = "ZCWX_PM";

            operationLogItem.opObjectID = "自产文献";
            // 操作涉及的书刊
            operationLogItem.opBookIn = "自产文献";

            // 时间
            operationLogItem.opDate = DateTime.Now;
            // 操作员
            operationLogItem.opPerson = uname;
            //记录操作模式，判断是否属于批量操作。取值范围SINGLE/BATCH。
            operationLogItem.opMode = "SINGLE";

            //操作的内容
            String content = "('" + zcwxPM.flbm + "','" + zcwxPM.sybm + "','" + zcwxPM.gch + "','" + zcwxPM.zwpm + "','" + zcwxPM.cbdw + "','" + zcwxPM.wxmj + "','" + zcwxPM.wxlx + "','" + zcwxPM.zrgr + "','" + zcwxPM.zrdw + "','" + zcwxPM.cbrq + "','" + zcwxPM.ztxt + "','" + zcwxPM.gtfl + "','" + zcwxPM.zwztc + "','" + zcwxPM.zwwz + "','" + zcwxPM.bz + "','" + zcwxPM.gxrq + "')";
            operationLogItem.opContent = content;
            return operationLogItem;
        }

        public ZCWX_DAO()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
    }
}